﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoRegisterAndActive;
namespace CadAutoRegisterAndActiveDemo
{
    public class Program : IExtensionApplication
    {
        private static bool m_isActive = false;

        private Database db;
        private Document doc;
        private Editor ed;

        private string cadVersion;
        private string dllPath;

        public Program()
        {
            doc = Application.DocumentManager.MdiActiveDocument;
            db = doc.Database;
            ed = doc.Editor;
            cadVersion = Application.Version.ToString();
            string[] strs = cadVersion.Split('.');
            cadVersion = strs[0] + "." + strs[1];
            dllPath = typeof(Program).Assembly.Location;
        }

        [CommandMethod("JH")]
        public void ActiveDllCommand()
        {
            ActiveDll();
        }

        public void Initialize()
        {
            AutoRegisterAndActiveModule.AutoRegister(cadVersion,dllPath);
            ed.WriteMessage("已成功注册到注册表中，下次使用无需netload加载\n");

            ActiveDll();
        }

        public void Terminate()
        {
            
        }
        [CommandMethod("HW")]
        public void Example()
        {
            if (!m_isActive)
            {
                ed.WriteMessage("程序未激活，请激活后使用，激活请联系HRDK");
                return;
            }
            ed.WriteMessage("Hello,World!!!");
        }


        private void ActiveDll()
        {
            DateTime overTime = DateTime.Now, registerTime = DateTime.Now;
            bool isActive;
            bool isOwnActiveCode = AutoRegisterAndActiveModule.CheckIsActive(cadVersion, dllPath, out string activeCode);
            if (isOwnActiveCode)
            {

                isActive = AutoRegisterAndActiveModule.CheckActive(activeCode, ref overTime, ref registerTime);
                if (isActive)
                {
                    m_isActive = isActive;
                    return;
                }
            }

            string machineCode = AutoRegisterAndActiveModule.CreateMachineCode();
            ed.WriteMessage($"\n{machineCode}\n");
            PromptResult pr = ed.GetString("请输入激活码，需要激活请联系HDRK");
            if (pr.Status != PromptStatus.OK)
            {
                return;
            }
            activeCode = pr.StringResult;
            isActive = AutoRegisterAndActiveModule.CheckActive(activeCode, ref overTime, ref registerTime);
            if (isActive)
            {
                m_isActive = isActive;
                ed.WriteMessage("激活成功");
                AutoRegisterAndActiveModule.AutoAvtive(cadVersion, dllPath, activeCode);
            }
            else
            {
                ed.WriteMessage("激活失败");
            }
        }
    }
}
